package com.fernfx.xingtan.user.ui;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.fernfx.xingtan.Constant;
import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.common.entity.user.MemberEntity;
import com.fernfx.xingtan.main.ui.MainActivity;
import com.fernfx.xingtan.user.AccountManager;
import com.fernfx.xingtan.user.contract.LoginContract;
import com.fernfx.xingtan.user.presenter.LoginPresenter;
import com.fernfx.xingtan.utils.LogUtils;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.RegexUtil;
import com.fernfx.xingtan.utils.SoftInputUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;
import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;

/**
 * Created by admin on 2018/7/31.
 * 登录
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener,LoginContract.View{
    @BindView(R.id.login_clt)
    ViewGroup loginView;

    @BindView(R.id.user_account_edit)
    EditText userAccountEdit;

    @BindView(R.id.user_pwd_edit)
    EditText userPwdEdit;

    //init presenter
    private LoginContract.Presenter presenter;
    //imei
    private String systemImei;

    public static void start(Context context){
        Intent loginIntent = new Intent(context,LoginActivity.class);
        context.startActivity(loginIntent);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_user_login;
    }

    @Override
    protected void initData() {
        presenter = new LoginPresenter<LoginContract.View>();
        presenter.init(this);
        systemImei = presenter.getSystemImei(mContext);
        //init account
        presenter.setLoginAccount(userAccountEdit);
    }

    @OnClick({R.id.login_clt,R.id.login_bt,R.id.forget_pwd_tv,R.id.register_tv})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_clt:
                SoftInputUtil.hideSoftInput(mContext,loginView);
                break;
            case R.id.login_bt:
                final String accountText = trimEditText(userAccountEdit);
                if (!(RegexUtil.isMobileNumber(accountText) || RegexUtil.isEmail(accountText))) {
                    OtherUtil.editTextRequestFocus(userAccountEdit);
                    ToastUtil.showCentertoast(R.string.account_error);
                    return;
                }
                final String pwdText = trimEditText(userPwdEdit);
                if (!RegexUtil.isPwd(pwdText)) {
                    OtherUtil.editTextRequestFocus(userPwdEdit);
                    ToastUtil.showCentertoast(R.string.pwd_error);
                    return;
                }
                //setting map
                requestArgsMap.clear();
                requestArgsMap.put("areaCode",Constant.RequestArgs.AREA_CODE);
                requestArgsMap.put("os","android");
                requestArgsMap.put("imei",systemImei);

                requestArgsMap.put("mobile",accountText);
                requestArgsMap.put("password",pwdText);
                //request
                showLoading();
                presenter.request(requestArgsMap);
                break;
            case R.id.register_tv:
                RegisterActivity.start(mContext);
                break;
            case R.id.forget_pwd_tv:
                ForgetPwdActivity.start(mContext);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SoftInputUtil.hideSoftInput(mContext,loginView);
        if (null != presenter) {
            presenter.detachView();
            presenter = null;
        }
    }

    @Override
    public BaseActivity getActivity() {
        return this;
    }

}
